//
//  MyHelpViewController.m
//  BGM
//
//  Created by liuqin on 2018/11/29.
//  Copyright © 2018年 com.alirtc.demo. All rights reserved.
//

#import "MyHelpViewController.h"
#import "PushMessageInfoViewController.h"

#import "MeHelpTableViewCell.h"
#import "Common.h"
#import "BGMHTTP.h"
#import "BGMUser.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"

@interface MyHelpViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *myTableView;
@property (nonatomic, strong)NSMutableArray *counts;

@property (nonatomic, assign)int currentpPage;


@end

@implementation MyHelpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navview.hidden = NO;
    self.titleBtn.hidden = NO;
    CGRect rect = self.titleBtn.frame;
    rect.size.width = rect.size.width+70;
    self.titleBtn.frame = rect;
    [self.titleBtn setTitle:self.title_Str forState:UIControlStateNormal];
    self.counts = [[NSMutableArray alloc]init];
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.navview.frame.size.height, SCREENW, SCREENH-self.navview.frame.size.height)];
    self.myTableView.backgroundColor = [UIColor clearColor];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    
  
    
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.counts removeAllObjects];
        self.currentpPage = 1;
        if (self.isAccpet) {  //我的受理成功
            [self requestAccpte];
        }else if (self.isForward){ //我的帮转
            [self requestForward];
        }
    }];
    
    self.myTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.currentpPage==0) {
            [self.myTableView.mj_footer endRefreshing];
        }else{
            if (self.isAccpet) {  //我的受理成功
                [self requestAccpte];
            }else if (self.isForward){ //我的帮转
                [self requestForward];
            }
        }
    }];
    
    [self.myTableView.mj_header beginRefreshing];
    
    
    
}
- (void)requestAccpte{
    NSDictionary *params = @{@"userToken":[BGMUser shareInstance].userToken,@"userId":[BGMUser shareInstance].userId,@"pageNum":@"1",@"pageSize":@"10"};
    [BGMHTTP requestWithMethodName:@"users/getUsersPublishAcceptanceList" parameters:params completion:^(BOOL success, NSError *error, NSDictionary *result) {
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString *code = [NSString stringWithFormat:@"%@",[result objectForKey:@"code"]];
            if ([code isEqualToString:@"200"]) {
                self.counts = [result objectForKey:@"data"];
                if (self.counts.count==0) {
                    self.myTableView.hidden = YES;
                    self.mormalLabel.hidden = NO;
                }else{
                    self.myTableView.hidden = NO;
                    self.mormalLabel.hidden = YES;
                    [self.myTableView reloadData];
                }
            }
        });
      
    }];
    
}

- (void)requestForward{
     NSDictionary *params = @{@"userToken":[BGMUser shareInstance].userToken,@"userId":[BGMUser shareInstance].userId,@"pageNum":[NSString stringWithFormat:@"%d",self.currentpPage],@"pageSize":@"10"};
    [BGMHTTP requestWithMethodName:@"users/getUsersPublishHelpList" parameters:params completion:^(BOOL success, NSError *error, NSDictionary *result) {
 
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString *code = [NSString stringWithFormat:@"%@",[result objectForKey:@"code"]];
            if ([code isEqualToString:@"200"]) {
                
                [self.myTableView.mj_header endRefreshing];
                [self.myTableView.mj_footer endRefreshing];

                NSArray *array = [result objectForKey:@"data"];
                [self.counts addObjectsFromArray:array];
                
                if (self.counts.count==0) {
                    self.myTableView.hidden = YES;
                    self.mormalLabel.hidden = NO;
                }else{
                    self.myTableView.hidden = NO;
                    self.mormalLabel.hidden = YES;
                    
                    if (array.count ==10) {
                        self.currentpPage = self.currentpPage+1;
                    }else{
                        self.currentpPage = 0;
                    }
                    
                    [self.myTableView reloadData];
                }
            }
        });
    }];
    
}

#pragma mark UITABLEVIEWDELEAGE
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.counts.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"CollectionTableViewCell";
    MeHelpTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[MeHelpTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSDictionary *dic =[self.counts objectAtIndex:indexPath.row];
    NSString *status = [NSString stringWithFormat:@"push_%@",[NSString stringWithFormat:@"%@",[dic objectForKey:@"ptype"]]];
    if ([status isEqualToString:@"1"]) {
        cell.resultLabel.text = @"进行中";
    }else{
        cell.resultLabel.text = @"已完成";
    }
    if (self.isAccpet) {  //我的帮忙完成
        cell.nameLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"uname"]];
        [cell.headImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[dic objectForKey:@"uHeadImage"]]] placeholderImage:[UIImage imageNamed:@"headImage"]];
        cell.timeLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"paTime"]];
        cell.messageLabel.text = [dic objectForKey:@"title"];
        [cell.commentBtn setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"commentsSize"]] forState:UIControlStateNormal];
        NSString *str1 = [NSString stringWithFormat:@"%@",[dic objectForKey:@"itisRedPackge"]];
        float red = [str1 floatValue];
        if (red>0) {
            cell.label1.hidden = NO;
        }else{
            cell.label1.hidden = YES;
        }
        float number = [[NSString stringWithFormat:@"%@",[dic objectForKey:@"pItIsPaid"]]intValue];
        if (number == 0) {
            cell.label2.text = @"有偿";
        }else{
            cell.label2.text = @"无偿";
        }
        NSString *pushType = [NSString stringWithFormat:@"%@",[dic objectForKey:@"ptId"]];
        NSString *imageStr = [NSString stringWithFormat:@"push_%@",pushType];
        [cell.statusImage setImage:[UIImage imageNamed:imageStr]];
        
        
    }else if (self.isForward){ //我的帮转
        [cell.headImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[dic objectForKey:@"headImage"]]] placeholderImage:[UIImage imageNamed:@"headImage"]];
        cell.timeLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"helpTime"]];
        cell.messageLabel.text = [dic objectForKey:@"ptitle"];
        [cell.commentBtn setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"commenSize"]] forState:UIControlStateNormal];
        cell.nameLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"uname"]];
        [cell.forwardBtn setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"forwardCount"]] forState:UIControlStateNormal];
        NSString *str1 = [NSString stringWithFormat:@"%@",[dic objectForKey:@"redPackgeNum"]];
        float red = [str1 floatValue];
        if (red>0) {
            cell.label1.hidden = NO;
        }else{
            cell.label1.hidden = YES;
        }
        float number = [[NSString stringWithFormat:@"%@",[dic objectForKey:@"pItIsPaid"]]intValue];
        if (number == 1) {
            cell.label2.text = @"有偿";
        }else{
            cell.label2.text = @"无偿";
        }
        
        NSString *pushType = [NSString stringWithFormat:@"%@",[dic objectForKey:@"ptId"]];
        NSString *imageStr = [NSString stringWithFormat:@"push_%@",pushType];
        [cell.statusImage setImage:[UIImage imageNamed:imageStr]];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dic =[self.counts objectAtIndex:indexPath.row];
    PushMessageInfoViewController *vc = [[PushMessageInfoViewController alloc]init];
    if ([dic objectForKey:@"pid"]) {
        vc.messageId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"pid"]];
    }else{
        vc.messageId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"pId"]];
    }
    vc.helpUsersId =@"";// [NSString stringWithFormat:@"%@",[dic objectForKey:@"helpId"]];
    vc.connectionUserId = @"";
    vc.isMe = YES;
    [self.navigationController pushViewController:vc animated:YES];

}



@end
